﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class BJXXDistribute : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindForm();
                BindEngineer();
            }
        }

        private void BindEngineer()
        {
            UserService service = new UserService();
            List<User> allEngineer = service.GetAllEngineer();

            engineerDropDownList.Items.Clear();
            engineerDropDownList.Items.Add(new ListItem("请选择"));

            foreach (User engineer in allEngineer)
            {
                engineerDropDownList.Items.Add(new ListItem(engineer.ContactPerson, engineer.UserID.ToString()));
            }
        }

        private void BindForm()
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ApplicationForm.UserReference.IsLoaded)
            {
                product.ApplicationForm.UserReference.Load();
            }
            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }

            contactLabel.Text = product.ApplicationForm.User.ContactPerson;
            companyLabel.Text = product.ApplicationForm.User.CompanyName;
            phoneNumberLabel.Text = product.ApplicationForm.User.UserPhone;
            cellPhoneLabel.Text = product.ApplicationForm.User.UserMobilePhone;
            faxLabel.Text = product.ApplicationForm.User.UserFax;
            addressLabel.Text = product.ApplicationForm.User.UserAddress;
            emailLabel.Text = product.ApplicationForm.User.UserEmail;
            agentLabel.Text = product.ApplicationForm.ProviderName;
            Label1.Text = product.ApplicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd");
            Label2.Text = product.BJXXReturnBack.HasValue ? product.BJXXReturnBack.Value.ToString("yyyy-MM-dd") : "";

            //
            runningNumberLabel.Text = product.ApplicationForm.RunningNumber;
            spcodeLabel.Text = product.SPCode;
            productTypeLabel.Text = product.ReturnProductType;
            productSerivalNumberLabel.Text = product.ReturnSerialNumber;
           // returnTimeLabel.Text = product.SendBackTime.HasValue ? product.SendBackTime.Value.ToString("yyyy-MM-dd") : "";
            buyTimeLabel.Text = product.ProductPurchaseDateTime.Value.ToString("yyyy-MM-dd");
            statusLabel.Text = product.Status;
            repairTupeLabel.Text = product.ProductRepairType;

            // bind last send product information
            if (product.ApplicationForm.ParentBJXXNO.HasValue)
            {
                ApplicationService appservice = new ApplicationService();
                ApplicationForm parentForm = appservice.GetApplicationFormByID(product.ApplicationForm.ParentBJXXNO.Value);
                if (parentForm != null && !parentForm.ApplicationProduct.IsLoaded)
                {
                    parentForm.ApplicationProduct.Load();
                }

                if (parentForm.ApplicationProduct.Count > 0)
                {
                    lastproductserialnumberTextBox.Text = parentForm.ApplicationProduct.FirstOrDefault().ReplaceProductSerialNumber;
                    lastproducttypeTextBox.Text = parentForm.ApplicationProduct.FirstOrDefault().ReplaceProductType;
                    lastproductVersionTextBox.Text = parentForm.ApplicationProduct.FirstOrDefault().ReplaceProductVersion;
                    lastsendRemarkTextBox.Text = parentForm.ApplicationProduct.FirstOrDefault().Remark;
                }

                reSendPanel.Visible = true;
            }

            //
            viewEngineerHyperLink.Attributes.Add("OnClick", "window.open('EngineerWorkCondition.aspx?" + Constants.QUERYSTR_STATION_ID + "=" + 1 + "')");
        }

        protected void backLinkButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("BJXXListNeedDistribute.aspx");
        }

        protected void okLinkButton_Click(object sender, EventArgs e)
        {
            if (engineerDropDownList.SelectedIndex == 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('请选择工程师');", true);
            }
            else
            {
                UserService userservice = new UserService();
                ApplicationProductService service = new ApplicationProductService();
                ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));

                product.DistributeDateTime = DateTime.Now;
                product.User = userservice.GetUserByID(long.Parse(engineerDropDownList.SelectedValue));
                product.Status = "备件已归还待验证";
                service.UpdateProduct(product);
                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('派单成功');location.href='BJXXListNeedDistribute.aspx';", true);
            }
        }
    }
}
